package com.wgj.android.accesslibility.service;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Build;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;

import com.wgj.android.BuildConfig;
import com.wgj.android.accesslibility.IStatusBarNotification;
import com.wgj.android.accesslibility.config.WXConfginUtil;
import com.wgj.android.accesslibility.config.WXPageUIManage;
import com.wgj.android.accesslibility.config.WXQiangHongBaoConfig;
import com.wgj.android.utils.NotifyHelper;

/**
 * ========================================
 * 作    者： 佳爱网络有限公司(wwww.jiaailove.com)
 * 版    本： 1.0
 * 创建日期：  2017/11/14
 * 描    述： 抢红包通知栏服务
 * 修订历吏：
 * =========================================
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public class WXQHBNotificationService extends NotificationListenerService {

    private static final String TAG = "QHBNotificationService";
    private  static  boolean isRuning=false;

    @Override
    public void onCreate() {
        super.onCreate();
        WXQiangHongBaoConfig.setIsEnableNotificationService(true);
        if(Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            onListenerConnected();
        }
    }
    @Override
    public void onNotificationPosted(final StatusBarNotification sbn) {
        if(sbn.getPackageName().equals(WXPageUIManage.WEIXIN_PACK_NAME)){
            String tickerText= String.valueOf(sbn.getNotification().tickerText);
            int index = tickerText.indexOf(":");
            if(index != -1) {
                tickerText = tickerText.substring(index + 1);
            }
            tickerText = tickerText.trim();
            if(tickerText.contains(WXPageUIManage.HONGBAO_TEXT_KEY)) { //红包消息
                WGJAccessServier.handeNotificationPosted(new IStatusBarNotification() {
                    @Override
                    public String getPackageName() {
                        return sbn.getPackageName();
                    }

                    @Override
                    public Notification getNotification() {
                        return sbn.getNotification();
                    }
                });
            }
        }





    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            super.onNotificationRemoved(sbn);
        }

    }

    @Override
    public void onListenerConnected() {
        isRuning = true;
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            super.onListenerConnected();
        }
        Log.i(TAG, "onListenerConnected");

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        WXQiangHongBaoConfig.setIsEnableNotificationService(false);
        isRuning=false;
    }

    /** 是否启动通知栏监听*/
    public static boolean isRunning() {
        return isRuning;
    }


}
